//
//  ACLiveViewController.m
//  ACLiteAVUISDK
//
//  Created by 冯琰琰 on 2021/3/29.
//

#import "ACLiveMianViewController.h"
#import "JXCategoryView.h"
#import "ACLiveViewController.h"
#import "ACPlayBackViewController.h"
#import "ACAppointmentLiveViewController.h"
#import "ACLiveTypeSelectedViewController.h"
#import "ACLiveCreateViewController.h"

@interface ACLiveMianViewController ()<JXCategoryViewDelegate,JXCategoryListContainerViewDelegate>
@property (nonatomic, strong) JXCategoryTitleView *myCategoryView;
@property (nonatomic, strong) JXCategoryBaseView *categoryView;
@property (nonatomic, strong) JXCategoryListContainerView *listContainerView;
@property (nonatomic, strong) NSArray *titles;
@property(nonatomic,assign)NSInteger selectedIndex;
@end

@implementation ACLiveMianViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    [self.view addSubview:self.categoryView];
    [self.view addSubview:self.listContainerView];
    
    self.titles = [[NSArray alloc] initWithObjects:@"精彩回放",@"直播", @"预约", nil];
    self.view.backgroundColor = [UIColor systemOrangeColor];
    self.myCategoryView.frame = CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, 44);
    self.myCategoryView.titles = self.titles;
    self.myCategoryView.titleColor = UIColorFromRGB(0x909399);
    self.myCategoryView.titleSelectedColor = UIColorFromRGB(0x303133);
    self.myCategoryView.titleFont = [UIFont systemFontOfSize:14];
    self.myCategoryView.titleSelectedFont = [UIFont systemFontOfSize:16 weight:UIFontWeightMedium];
    self.myCategoryView.titleLabelMaskEnabled = YES;
    self.myCategoryView.defaultSelectedIndex = 1;
    
    JXCategoryIndicatorLineView *lineView = [[JXCategoryIndicatorLineView alloc] init];
    lineView.indicatorWidth = JXCategoryViewAutomaticDimension;
    self.myCategoryView.indicators = @[lineView];
    self.myCategoryView.titleColorGradientEnabled = YES;
    [self.myCategoryView removeFromSuperview];
    self.navigationItem.titleView = self.myCategoryView;
    
    
    UIButton *createLiveBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [createLiveBtn setImage:[UIImage imageWithName:@"icon_live_create" bundle:ACLiteAVUISDK targetClass:[self class]] forState:UIControlStateNormal];
    [self.view addSubview:createLiveBtn];
    [createLiveBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.size.mas_equalTo(CGSizeMake(50, 50));
        make.right.equalTo(self.view.mas_right).offset(-15);
        make.bottom.equalTo(self.view.mas_bottom).offset(-20);
    }];
    @weakify(self)
    [[createLiveBtn rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(__kindof UIControl * _Nullable x) {
        @strongify(self)
        ACLiveTypeSelectedViewController *selectedVC = [[ACLiveTypeSelectedViewController alloc] init];
        selectedVC.modalPresentationStyle = UIModalPresentationOverFullScreen & UIModalPresentationOverCurrentContext;
        [self presentViewController:selectedVC animated:YES completion:nil];
        [selectedVC.subject subscribeNext:^(id  _Nullable x) {
            NSInteger value = [x integerValue];
            if (value == -1) {
                // 弹窗失败
                [SVProgressHUD showErrorWithStatus:@"请选择直播类型"];
            }else{
                self.selectedIndex = value;
                ACLiveCreateViewController *createVC = [[ACLiveCreateViewController alloc] init];
                createVC.liveType = self.selectedIndex;
                [self.navigationController pushViewController:createVC animated:YES];
            }
        } completed:^{
            
        }];
    }];
}

- (void)viewDidLayoutSubviews {
    [super viewDidLayoutSubviews];
    self.categoryView.frame = CGRectMake(0, 0, self.view.bounds.size.width, [self preferredCategoryViewHeight]);
    self.listContainerView.frame = CGRectMake(0, 0, self.view.bounds.size.width, self.view.bounds.size.height);
}

// 分页菜单视图
- (JXCategoryBaseView *)categoryView {
    if (!_categoryView) {
        _categoryView = [self preferredCategoryView];
        _categoryView.delegate = self;
        _categoryView.listContainer = self.listContainerView;
    }
    return _categoryView;
}
-(JXCategoryTitleView *)myCategoryView{
    return (JXCategoryTitleView *)self.categoryView;;
}
- (JXCategoryBaseView *)preferredCategoryView {
    return [[JXCategoryTitleView alloc] init];
}
// 列表容器视图
- (JXCategoryListContainerView *)listContainerView {
    if (!_listContainerView) {
        _listContainerView = [[JXCategoryListContainerView alloc] initWithType:JXCategoryListContainerType_ScrollView delegate:self];
    }
    return _listContainerView;
}

- (CGFloat)preferredCategoryViewHeight {
    return 64;
}
#pragma mark - JXCategoryViewDelegate
// 点击选中或者滚动选中都会调用该方法。适用于只关心选中事件，不关心具体是点击还是滚动选中的。
- (void)categoryView:(JXCategoryBaseView *)categoryView didSelectedItemAtIndex:(NSInteger)index {
    DDLogInfo(@"%@", NSStringFromSelector(_cmd));
    // 侧滑手势处理
    self.navigationController.interactivePopGestureRecognizer.enabled = (index == 0);
}

// 滚动选中的情况才会调用该方法
- (void)categoryView:(JXCategoryBaseView *)categoryView didScrollSelectedItemAtIndex:(NSInteger)index {
    DDLogInfo(@"%@", NSStringFromSelector(_cmd));
}

#pragma mark - JXCategoryListContainerViewDelegate

// 返回列表的数量
- (NSInteger)numberOfListsInlistContainerView:(JXCategoryListContainerView *)listContainerView {
    return self.titles.count;
}

// 返回各个列表菜单下的实例，该实例需要遵守并实现 <JXCategoryListContentViewDelegate> 协议
- (id<JXCategoryListContentViewDelegate>)listContainerView:(JXCategoryListContainerView *)listContainerView initListForIndex:(NSInteger)index {
    if (index == 0) {
        ACPlayBackViewController *VC = [[ACPlayBackViewController alloc] init];
        return VC;
    }else if (index == 1){
        ACLiveViewController *VC = [[ACLiveViewController alloc] init];
        return VC;
    }else{
        ACAppointmentLiveViewController *VC = [[ACAppointmentLiveViewController alloc] init];
        return VC;
    }
}


@end
